<?php 

require_once '../data/connect.class.php';

class UserOpt {
	
	var $connection;
	function UserOpt($conn) {
		$this->connection = $conn;
	}
	function removeUser($id) {
		$array = Array('user_id'=>$id);
		$this->connection->delSql("admin_page_user",$array);
		$array = Array('id'=>$id);
		$this->connection->delSql("admin_user",$array);
	}
	function addUser($name,$password) {
		$sql="insert into admin_user(name,password) values ('".$name."','".$password."')";
		$this->connection->execute($sql);
	}
	function addPrivilege($userId,$privilegeArr) {
		$userSql = "select name from admin_user where id='".$userId."'";
		$resultSet = $this->connection->execute($userSql);
		$userName = $resultSet[0]['name'];
		
		$array = Array('user_id'=>$userId);
		$this->connection->delSql("admin_page_user",$array);
		
		foreach($privilegeArr as $privilege) {
			if (strlen(trim($privilege)) > 0) {
				$sql="insert into admin_page_user(user_id,user_name,page_id) values (".$userId.",'".$userName."',".$privilege.")";
			    $this->connection->execute($sql);
				
			}
		}
	}
}

$connect = new Connect("localhost","root","","email");
$userOpt = new UserOpt($connect);
$opt=$_POST['opt'];
if (isset($opt)) {
	if ($opt=='del') {
		$id = $_POST['id'];
		$userOpt->removeUser($id);
	}
    if ($opt=='add') {
		$name = $_POST['name'];
		$password = $_POST['password'];
		
		$userOpt->addUser($name,$password);
	}
	if ($opt=='privilege') {
		$privileges = $_POST['privileges'];
		$userId = $_POST['userId'];
		$strArray = explode(',',$privileges);
		$userOpt->addPrivilege($userId,$strArray);
		
	}
}

?>